// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import Qt5Compat.GraphicalEffects
import Fk
import Fk.PhotoElement
import Fk.RoomElement

/* Layout of general card:
 *      +--------+
 *kindom|wu  9999| <- hp
 *name -|s       |
 *      |q img   |
 *      |        |
 *      |        |
 *      +--------+
 * Inherit from CardItem to use common signal
 */

// 大量删减后的GeneralCardItem，同时附加了一些额外显示功能

CardItem {
  property string pkgName: ""
  property string back: ""
  property bool detailed: true
  property var footnote
  property var lord
  property bool hide: false
  
  name: ""
  suit: ""
  number: 0
  known: (Self.id === lord) || !hide
  card.source: {
    const data = lcall("GetGeneralData", name);
    const extension = data.extension;
    if (known) return SkinBank.getGeneralPicture(name);
    const path = AppPath + "/packages/" + extension + "/image/generals/"
              + back + ".jpg";
    if (Backend.exists(path)) {
      return path;
    }
    return SkinBank.GENERAL_DIR + "0.jpg";
  }
  glow.color: "white"

  Image {
    source: known ? (AppPath + "/packages/luming/image/border" + (((Self.id === lord) && hide) ? "" : "2")) : ""
  }

  Text {
    width: 20
    height: 80
    x: 2
    y: lineCount > 6 ? 30 : 34
    text: name !== "" ? luatr(name) : "nil"
    visible: luatr(name).length <= 6 && known
    color: "white"
    font.family: fontLibian.name
    font.pixelSize: 18
    lineHeight: Math.max(1.4 - lineCount / 8, 0.6)
    style: Text.Outline
    wrapMode: Text.WrapAnywhere
  }

  Text {
    x: 0
    y: 12
    rotation: 90
    transformOrigin: Item.BottomLeft
    text: luatr(name)
    visible: luatr(name).length > 6 && known
    color: "white"
    font.family: fontLibian.name
    font.pixelSize: 18
    style: Text.Outline
  }

  Rectangle {
    visible: pkgName !== "" && known
    height: 16
    width: childrenRect.width + 4
    anchors.bottom: parent.bottom
    anchors.bottomMargin: 4
    anchors.right: parent.right
    anchors.rightMargin: 4

    color: "#3C3229"
    opacity: 0.8
    radius: 4
    border.color: "white"
    border.width: 1
    Text {
      text: luatr(pkgName)
      x: 2; y: 1
      font.family: fontLibian.name
      font.pixelSize: 14
      color: "white"
      style: Text.Outline
      textFormat: Text.RichText
    }
  }

  Text {
    id: footnoteItem
    text: {
      const marks = footnote
      const txt = [];
      for (const mark in marks) {
        if (mark.startsWith("@@")) {
          txt.push(`${luatr(mark)}`);
        } else if (mark.startsWith('@[')) {
          const close_br = mark.indexOf(']');
          if (close_br !== -1) {
            const mark_type = mark.slice(2, close_br);
            data = JSON.stringify(data);
            const _data = lcall("GetQmlMark", mark_type, mark, data,
                                root.parent?.playerid);
            if (_data && _data.text && _data.text !== "#hidden") {
              txt.push(`${luatr(mark)} ${luatr(_data.text)}`);
            }
          }
        } else if (mark.startsWith("@")) {
          txt.push(`${luatr(mark)} ${luatr(marks[mark])}`);
        }
      }
      // if ((Self.id === lord) && hide) {
      //   txt.push(`${luatr(`lm_ishide`)}`)
      // }

      return txt.join("<br>");
    }
    x: 0
    y: parent.height - height - 10
    width: parent.width
    color: "#E4D5A0"
    // color: "white"
    visible: known
    style: Text.Outline
    wrapMode: Text.WrapAnywhere
    textFormat: Text.RichText
    horizontalAlignment: Text.AlignHCenter
    font.family: fontLibian.name
    font.pixelSize: 14
    // glow.color: "black"
    // glow.spread: 1
    // glow.radius: 1
    //glow.samples: 12
  }

  onNameChanged: {
    const splited = name.split("__");
    if (splited.length > 1) {
      pkgName = splited[0];
    } else {
      pkgName = "";
    }
  }
}
